﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxTreeList;

public partial class ConsumerManage_CustomerAssociation : Frame.Pages.BasePage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!IsPostBack)
        {
            Session["ConsumerManage_CustomerAssociation_uid"] = null;
        }
        DDataBind();
    }

    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (e.Parameter.Equals("null"))
        {
            throw new Exception("请选择人员！");
        }

        int uid = int.Parse(e.Parameter);
        //清空现有关联
        initConsumer(uid);

        if (treelist.GetSelectedNodes().Count > 0)
        {
            List<TreeListNode> selection = treelist.GetSelectedNodes();

            foreach (TreeListNode node in selection)
            {
                int conID = Convert.ToInt32(node.GetValue("ConsumerID"));
                using (DAL.CMSDALDataContext dataContext = new DAL.CMSDALDataContext())
                {
                    DAL.BI_Consumer consumer = dataContext.BI_Consumer.First(o => o.ConsumerID == conID);
                    if (consumer != null)
                    {
                        consumer.UserID = uid;
                    }
                    dataContext.SubmitChanges();
                }
            }
        }
    }

    //初始化客户，清空编号为uid的人员现有所以客户关联
    void initConsumer(int uid)
    {
        using (DAL.CMSDALDataContext dataContext = new DAL.CMSDALDataContext())
        {
            var db = dataContext.BI_Consumer.Where(o => o.UserID == uid);
            foreach (DAL.BI_Consumer consumer in db)
            {
                consumer.UserID = -1;
            }
            dataContext.SubmitChanges();
        }
    }

    protected void treelist_CustomCallback(object sender, DevExpress.Web.ASPxTreeList.TreeListCustomCallbackEventArgs e)
    {
        int uid = int.Parse(e.Argument);
        Session["ConsumerManage_CustomerAssociation_uid"] = uid;
        DDataBind();
        using (DAL.CMSDALDataContext dataContext = new DAL.CMSDALDataContext())
        {
            var db = dataContext.BI_Consumer.Where(o => o.UserID == uid);
            //清除所有选择
            for (int i = 0; i < treelist.Nodes.Count; i++)
            {
                treelist.Nodes[i].Selected = false;
            }

            if (db != null)
            {
                foreach (DAL.BI_Consumer consumer in db)
                {
                    TreeListNode SelectedNode = treelist.FindNodeByKeyValue(consumer.ConsumerID.ToString());

                    if (SelectedNode != null)
                    {
                        SelectedNode.Selected = true;
                    }
                }
            }
        }
    }

    void DDataBind()
    {
        using (DAL.CMSDALDataContext dataContext = new DAL.CMSDALDataContext())
        {
            if (Session["ConsumerManage_CustomerAssociation_uid"] != null)
            {
                int uid = (int)Session["ConsumerManage_CustomerAssociation_uid"];
                treelist.DataSource = dataContext.BI_Consumer.Where(o => o.UserID == uid || o.UserID == -1);
            }
            else
            {
                treelist.DataSource = dataContext.BI_Consumer;
            }
            treelist.DataBind();
        }
    }

    protected override string ActionID
    {
        get { return "0"; }
    }
}